<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList"/>
    </el-row>


    <el-table v-loading="loading" :data="dataList" border>
      <el-table-column label="ID" prop="id" width="120px"/>
      <el-table-column label="培训计划名称" prop="planName"/>
      <el-table-column label="起止时间">
        <template slot-scope="scope">
          <div>
            起始：{{ scope.row.startTime }}
          </div>
          <div>
            结束：{{ scope.row.endTime }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="考官" prop="offlineExamLecturer"/>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_status"
                    :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="考试状态">
        <template slot-scope="scope">
          <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_offline_status"
                    :value="scope.row.examStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-info"
            size="mini"
            type="text"
            @click="handleInfo(scope.row.id)">详情
          </el-button>
          <el-button
            :disabled="scope.row.status !== '1' || scope.row.examStatus === '2'"
            icon="el-icon-success"
            size="mini"
            type="text"
            @click="handleSuccess(scope.row)">完成考试
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <el-dialog :visible.sync="modal" title="完成线下考试">
      <el-form :data="data" label-position="top">
        <el-form-item label="* 考试评分">
          <div style="margin-bottom: 10px; display: flex; justify-content: space-between">
            <div>
              <el-button type="success" plain size="mini" @click="batchUpdateStatus('1')">
                全部合格
              </el-button>
              <el-button type="danger" plain size="mini" @click="batchUpdateStatus('0')">
                全部不合格
              </el-button>
            </div>
            <div style="display: flex; align-items: center">
              全部
              <el-input-number v-model="score" style="margin: 0 5px" size="mini" :precision="2" :step="1" :min="0"
                               :max="100"/>
              分
              <el-link @click="batchUpdateScore" style="margin-left: 5px" :underline="false" icon="el-icon-success"/>
            </div>
          </div>
          <el-table :data="form.users" size="mini" border max-height="400">
            <el-table-column label="用户名" prop="nickName">
              <template slot-scope="scope">
                <div style="display: flex; align-items: center">
                  <el-avatar size="small" :src="scope.row.avatar" @error="false">
                    {{ scope.row.nickName[0] }}
                  </el-avatar>
                  <span style="margin-left: 5px">{{ scope.row.nickName }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="状态">
              <template slot-scope="scope">
                <el-select v-model="scope.row.status" size="mini">
                  <el-option
                    v-for="item in dict.type.train_plan_offline_exam_status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="评分">
              <template slot-scope="scope">
                <el-input-number size="mini" v-model="scope.row.score" :precision="2" :step="1" :min="0" :max="100"/>
              </template>
            </el-table-column>
            <el-table-column label="备注">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.remark" type="textarea" placeholder="请输入备注"/>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" placeholder="请输入备注" v-model="form.remark"/>
        </el-form-item>
        <el-form-item label="* 附件">
          <fileUpload @input="getFiles" :limit="20" :file-size="20"
                      :file-type="['jpg', 'png', 'xls', 'xlsx', 'ppt', 'pptx', 'doc', 'docx', 'mp4', 'mp3']"/>
        </el-form-item>
      </el-form>

      <div style="text-align: right;">
        <el-button @click="modal = false">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="submitLoading">{{
            submitLoading ? '提交中 ...' : '确 定'
          }}
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="infoModal" title="线下考试详情" width="45%">
      <el-descriptions title="培训计划信息" column="1">
        <el-descriptions-item label="计划名称">{{ data.planName }}</el-descriptions-item>
        <el-descriptions-item label="起止时间">{{data.startTime}} 至 {{data.endTime}}</el-descriptions-item>
        <el-descriptions-item label="负责人">{{data.offlineExamLecturer}}</el-descriptions-item>
        <el-descriptions-item label="考官">{{data.offlineExamLecturer}}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_status"
                    :value="data.status"/>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{data.createTime}}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions title="考试信息" column="1">
        <el-descriptions-item label="考试状态">
          <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_offline_status"
                    :value="data.examInfo.status"/>
        </el-descriptions-item>
        <el-descriptions-item label="操作时间">{{data.examInfo.optTime}}</el-descriptions-item>
        <el-descriptions-item label="备注">{{data.examInfo.remark}}</el-descriptions-item>
        <el-descriptions-item label="附件">
          <div style="display: flex; flex-direction: column">
            <div v-for="(f,i) in data.examInfo.fileList" :key="i" style="width: 500px; height: 40px; border: 1px solid #d8c0c0; border-radius: 5px; display: flex; padding: 0 10px; align-items: center; margin-bottom: 10px">
              <el-link :href="f.url" target="_blank">{{f.originalName}}</el-link>
            </div>
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="成绩">
          <el-table :data="data.examInfo.userList" size="mini" border max-height="400" style="width: 200px">
            <el-table-column label="用户名" prop="nickName"/>
            <el-table-column label="状态">
              <template slot-scope="scope">
                <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_offline_exam_status"
                          :value="scope.row.status"/>
              </template>
            </el-table-column>
            <el-table-column label="评分" prop="score"/>
            <el-table-column label="备注" prop="remark"/>
          </el-table>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import fileUpload from '@/components/FileUpload/index.vue'
import {finishOfflineExam, getOfflineExamInfo, getOfflineExamPlans} from "@/api/edu/trainPlan";

export default {
  name: 'offlineExam',
  dicts: ['train_plan_status', 'train_plan_offline_exam_status', 'train_plan_offline_status'],
  components: {fileUpload},
  data() {
    return {
      loading: false,
      submitLoading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      dataList: [],
      data: {
        examInfo: {}
      },
      modal: false,
      infoModal: false,
      checkedUserIds: [],
      form: {},
      score: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getOfflineExamPlans(this.queryParams).then(res => {
        this.dataList = res.data.list
        this.total = res.data.total
        this.loading = false
      })
    },
    handleInfo(id) {
      getOfflineExamInfo(id).then(res => {
        this.data = res.data
        this.infoModal = true
      })
    },
    handleSuccess(row) {
      this.form = {...row}
      this.modal = true
    },
    getFiles(files) {
      this.form.files = files
    },
    batchUpdateStatus(status) {
      this.form.users.forEach(u => {
        u.status = status
      })
    },
    batchUpdateScore() {
      this.form.users.forEach(u => {
        u.score = this.score
      })
    },
    submit() {
      console.log(this.form)
      this.submitLoading = true
      this.form.trainPlanId = this.form.id
      this.form.completeUserIds = this.checkedUserIds.toString()
      finishOfflineExam(this.form).then(res => {
        this.submitLoading = false
        this.modal = false
        this.getList()
      })
    }
  }
}
</script>

<style scoped>

</style>
